import axios from "axios";
import * as Config from '@/settings'
import {getToken} from '@/utils/auth'
import {ElMessage} from "element-plus";
import {logout} from "@/api/login";

export const request = axios.create({
    "baseURL": '/',
    "timeout": Config.timeout // 请求超时时间
})

//请求拦截器
request.interceptors.request.use(config => {
        if (getToken()) {
            config.headers['Authorization'] = getToken()
        }
        config.headers['Content-Type'] = 'application/json'
        return config
    },
    error => {

    return Promise.reject(error)
    })
//添加响应拦截器
request.interceptors.response.use(response=>{
return response.data
},error => {
    let code = error.response.data.status
    ElMessage.error(code + ' ' + error.response.data.message)
    if (code === 401) {//如果认证失败，则实行注销操作
        logout()
    }
    return Promise.reject(error)
    }
)

